/*
 * @Author: 0x9DEFA478
 * @Date: 2022-02-14 22:08:03
 * @LastEditTime: 2022-08-21 14:28:24
 * @LastEditors: 0x9DEFA478
 * @Description: 
 * QQ:2652450237
 * ============================================================================================================================================
 * 
 * 
 * 
 *                                                                                               ************      ****************************
 *                                                                                             ************      ****************************  
 *                                                                                           ************      ****************************    
 *                                                                                         ************      ****************************      
 *                                                                                       ************      ************                        
 *                                                                                     ************      ************                          
 *                                                                                   ************      ************                            
 *                                                                                 ************      ************                              
 *                                                                               ************      ************                                
 *                                                                             ************      ************                                  
 *                                                                           ************      ************                                    
 *                                                                         ************      ************                                      
 *                                                                       ************      ************                                        
 *                                                                     ************      ************                                          
 *                                                                   ************      ************                                            
 *                                                                 ************      ************                                              
 *                                                               ************      ************                                                
 *                                                             ************      ************                                                  
 *                                                           ************      ************                                                    
 *                                                         ************      ************                                                      
 *                                                       ************      ************                                                        
 *                                                     ************      ************                                                          
 *                                                   ************      ************                                                            
 *                                                 ************      ************                                                              
 *                                               ************      ************                                                                
 *                                             ************      ************                                                                  
 *                                           ************      ************                                                                    
 *                                         ************      ************                                                                      
 *                                       ************      ************                                                                        
 *                                     ************      ************                                                                          
 *                                   ************      ************                                                                            
 *                                 ************      ************                                                                              
 *                               ************      ************                                                                                
 *                             ************      ************                                                                                  
 *                           ************      ************                                                                                    
 *                         ************      ************                                                                                      
 *       ****************************      ************                                                                                        
 *     ****************************      ************                                                                                          
 *   ****************************      ************                                                                                            
 * ****************************      ************                                                                                              
 * 
 * 
 * 
 * ============================================================================================================================================
 * 
 */
#include "../H_NES_Mapper.h"




extern void H_NES_Mapper85_PRG_Write(Hbyte Value,Huint16 Address,void* v);
extern Hbyte H_NES_Mapper85_PRG_Read(Huint16 Address,void* v);
extern Huint16 H_NES_Mapper85_PRG_Read16(Huint16 Address,void* v);
extern void H_NES_Mapper85_CHR_Exception_Write(Hbyte Value,Huint16 Address,void* v);
extern void H_NES_Mapper85_CHR_Write(Hbyte Value,Huint16 Address,void* v);
extern Hbyte H_NES_Mapper85_CHR_Read(Huint16 Address,void* v);
extern void H_NES_Mapper85_CPUTick(int CPUCycle,void* v);




#define ThisMapper Cartridge.Mapper.SpecificMapper.Mapper85




void H_NES_Mapper85_MapperUpdate(void* v){
  H_NES_Console* Console;
  Hbyte* p;
  Huint32 ROM_Size;
  Huint32 Offset;
  Hbyte* t_bytep;
  int i;

  Console=v;
  
  p=Console->Cartridge.Mapper.PRG_ROM;
  ROM_Size=Console->Cartridge.Mapper.PRG_ROM_Size;

  for(i=0;i<3;i++){
    Offset=((Huint32)Console->ThisMapper.Reg_PRGSEL[i])<<13;
    Offset=Offset%ROM_Size;
    t_bytep=&p[Offset];
    Console->ThisMapper.PRG_Banks.Base8000_8KB[i]=t_bytep;
  }

  p=Console->Cartridge.Mapper.CHR_ROM;
  ROM_Size=Console->Cartridge.Mapper.CHR_ROM_Size;
  if(ROM_Size==0U){
    ROM_Size=0x2000U;
  }
  for(i=0;i<8;i++){
    Offset=(Huint32)Console->ThisMapper.Reg_CHRSEL[i];
    Offset<<=10;
    Offset=Offset%ROM_Size;
    t_bytep=&p[Offset];
    Console->ThisMapper.CHR_Banks.Base0000_1KB[i]=t_bytep;
  }

}

void H_NES_Mapper85_PRG_Write_Ext(Hbyte Value,Huint16 Address,H_NES_Console* Console){

  Address=(Address&0xF000U)
    |(((Address&(Huint16)Console->ThisMapper.MskBit)!=0U)?0x0008U:0U);

  switch(Address&0xF008U){
    case 0x8000U:
      Console->ThisMapper.Reg_PRGSEL[0]=Value;
      H_NES_Mapper85_MapperUpdate(Console);
      break;
    case 0x8008U:
      Console->ThisMapper.Reg_PRGSEL[1]=Value;
      H_NES_Mapper85_MapperUpdate(Console);
      break;
    case 0x9000U:
      Console->ThisMapper.Reg_PRGSEL[2]=Value;
      H_NES_Mapper85_MapperUpdate(Console);
      break;
    case 0xE000U:
      if(Console->PPU.VRAM_ScreenType!=0x03U){
        switch(Value&0x03U){
          case 0x0000U:
            //命名表水平排列
            Console->PPU.NameTable[0]=&((Hbyte*)Console->PPU.VRAM)[0];
            Console->PPU.NameTable[1]=&((Hbyte*)Console->PPU.VRAM)[0x400];
            Console->PPU.NameTable[2]=&((Hbyte*)Console->PPU.VRAM)[0];
            Console->PPU.NameTable[3]=&((Hbyte*)Console->PPU.VRAM)[0x400];
            Console->PPU.VRAM_ScreenType=0x02U;
            break;
          case 0x0001U:
            //命名表垂直排列
            Console->PPU.NameTable[0]=&((Hbyte*)Console->PPU.VRAM)[0];
            Console->PPU.NameTable[1]=&((Hbyte*)Console->PPU.VRAM)[0];
            Console->PPU.NameTable[2]=&((Hbyte*)Console->PPU.VRAM)[0x400];
            Console->PPU.NameTable[3]=&((Hbyte*)Console->PPU.VRAM)[0x400];
            Console->PPU.VRAM_ScreenType=0x01U;
            break;
          case 0x0002U:
            Console->PPU.NameTable[0]=&((Hbyte*)Console->PPU.VRAM)[0];
            Console->PPU.NameTable[1]=&((Hbyte*)Console->PPU.VRAM)[0];
            Console->PPU.NameTable[2]=&((Hbyte*)Console->PPU.VRAM)[0];
            Console->PPU.NameTable[3]=&((Hbyte*)Console->PPU.VRAM)[0];
            Console->PPU.VRAM_ScreenType=0x00U;
            break;
          case 0x0003U:
            Console->PPU.NameTable[0]=&((Hbyte*)Console->PPU.VRAM)[0x400];
            Console->PPU.NameTable[1]=&((Hbyte*)Console->PPU.VRAM)[0x400];
            Console->PPU.NameTable[2]=&((Hbyte*)Console->PPU.VRAM)[0x400];
            Console->PPU.NameTable[3]=&((Hbyte*)Console->PPU.VRAM)[0x400];
            Console->PPU.VRAM_ScreenType=0x00U;
            break;
          default:
            H_NES_Mapper_Warning(Console,vH_NES_Result_PRG_WriteFail,H_NES_ConstString_ExceptionPRGWriteAccess,Address);
            break;
        }
      }
      break;
    case 0xE008U:
      Console->ThisMapper.CountReload=Value;
      break;
    case 0xF000U:
      Console->ThisMapper.Reg_F000=Value;
      if((Value&0x02U)!=0x00U){
        Console->ThisMapper.Count=Console->ThisMapper.CountReload;
      }
      Console->ThisMapper.DivCount=341;
      Console->CPU.Status&=(~vH_NES_CPU_Status_IRQ_ExtIRQ);
      Console->ThisMapper.IRQ_DelayCount=0;
      break;
    case 0xF008U:
      Value=Console->ThisMapper.Reg_F000;
      Console->ThisMapper.Reg_F000=(Value&(~0x02U))|((Value<<1)&0x02U);
      Console->CPU.Status&=(~vH_NES_CPU_Status_IRQ_ExtIRQ);
      Console->ThisMapper.IRQ_DelayCount=0;
      break;
    default:
      if((Address>0x9FFFU)&&(Address<0xE000U)){
        //0xA000~0xDFFF
        Console->ThisMapper.Reg_CHRSEL[((Address>>3)&0x0001U)+((Address-0xA000U)>>11)]=Value;
        H_NES_Mapper85_MapperUpdate(Console);
      }
      break;
  }
}




static void GetProgress(void* v,void* Progress){
  H_NES_Console* Console;
  Hbyte* p;
  int i;
  
  Console=v;
  p=Progress;

  H_NES_Mapper_DefaultGetProgress(Console,p);

  p=&p[H_NES_Mapper_DefaultProgressSize(Console)];

  p[0]=Console->ThisMapper.Reg_PRGSEL[0];
  p[1]=Console->ThisMapper.Reg_PRGSEL[1];
  p[2]=Console->ThisMapper.Reg_PRGSEL[2];

  for(i=0;i<8;i++){
    p[3+i]=Console->ThisMapper.Reg_CHRSEL[i];
  }

  p[11]=Console->ThisMapper.Reg_E000;

  p[12]=Console->ThisMapper.Reg_F000;
  p[13]=Console->ThisMapper.Count;
  p[14]=Console->ThisMapper.CountReload;
  p[15]=((Hbyte*)&Console->ThisMapper.DivCount)[0];
  p[16]=((Hbyte*)&Console->ThisMapper.DivCount)[1];

  p[17]=Console->ThisMapper.IRQ_DelayCount;

}

static void LoadProgress(void* v,void* Progress){
  H_NES_Console* Console;
  Hbyte* p;
  int i;
  
  Console=v;
  p=Progress;

  H_NES_Mapper_DefaultLoadProgress(Console,p);

  p=&p[H_NES_Mapper_DefaultProgressSize(Console)];

  
  Console->ThisMapper.Reg_PRGSEL[0]=p[0];
  Console->ThisMapper.Reg_PRGSEL[1]=p[1];
  Console->ThisMapper.Reg_PRGSEL[2]=p[2];

  for(i=0;i<8;i++){
    Console->ThisMapper.Reg_CHRSEL[i]=p[3+i];
  }

  Console->ThisMapper.Reg_E000=p[11];

  Console->ThisMapper.Reg_F000=p[12];
  Console->ThisMapper.Count=p[13];
  Console->ThisMapper.CountReload=p[14];
  ((Hbyte*)&Console->ThisMapper.DivCount)[0]=p[15];
  ((Hbyte*)&Console->ThisMapper.DivCount)[1]=p[16];

  Console->ThisMapper.IRQ_DelayCount=p[17];
  
  H_NES_Mapper85_MapperUpdate(Console);
}

int H_NES_Mapper85_Init(void* v){
  H_NES_Console* Console;
  int i;

  Console=v;


  if(Console->Cartridge.Mapper.PRG_RAM_Size<=0){//总是提供RAM
    Console->Cartridge.Mapper.PRG_RAM_Size=0x2000;
    Console->Cartridge.Mapper.PRG_RAM=Console->Memory.Malloc(0x2000);
    if(Console->Cartridge.Mapper.PRG_RAM==NULL){
      return vH_NES_Result_PRG_RAM_AllocFail;
    }
  }


  switch(Console->SubMapperNumber){
    case 1:
      Console->ThisMapper.MskBit=0x08U;
      break;
    case 2:
      Console->ThisMapper.MskBit=0x10U;
      break;
    default:
      Console->ThisMapper.MskBit=0x18U;
      break;
  }

  for(i=0;i<3;i++){
    Console->ThisMapper.Reg_PRGSEL[i]=(Hbyte)i;
  }
  for(i=0;i<8;i++){
    Console->ThisMapper.Reg_CHRSEL[i]=(Hbyte)i;
  }
  Console->ThisMapper.Reg_E000=0x01U;

  Console->ThisMapper.Reg_F000=0x00U;
  Console->ThisMapper.Count=0x00U;
  Console->ThisMapper.CountReload=0x00U;
  Console->ThisMapper.DivCount=341;

  Console->ThisMapper.IRQ_DelayCount=0;

  Console->ThisMapper.PRG_Banks.Base8000_8KB[3]=&Console->Cartridge.Mapper.PRG_ROM[Console->Cartridge.Mapper.PRG_ROM_Size-0x2000U];

  H_NES_Mapper85_MapperUpdate(Console);


  Console->Cartridge.Mapper.DeInit=NULL;

  
  Console->Cartridge.PRG_Write=H_NES_Mapper85_PRG_Write;
  Console->Cartridge.PRG_Read=H_NES_Mapper85_PRG_Read;
  Console->Cartridge.PRG_Read16=H_NES_Mapper85_PRG_Read16;
  
  if(Console->Cartridge.Mapper.CHR_ROM_Size==0U){
    Console->Cartridge.CHR_Write=H_NES_Mapper85_CHR_Write;
  }else{
    Console->Cartridge.CHR_Write=H_NES_Mapper85_CHR_Exception_Write;
  }
  Console->Cartridge.CHR_Read=H_NES_Mapper85_CHR_Read;

  Console->Cartridge.Progress.Size=18+H_NES_Mapper_DefaultProgressSize(Console);
  Console->Cartridge.Progress.Get=GetProgress;
  Console->Cartridge.Progress.Load=LoadProgress;

  Console->Cartridge.Option.CPUTick=H_NES_Mapper85_CPUTick;
  
  

  return vH_NES_Result_OK;
}




#undef ThisMapper



